package com.it.controller;

import cn.hutool.core.date.DateUtil;
import com.it.entity.Log;
import com.it.entity.User;
import com.it.service.LogService;
import com.it.service.UserService;
import com.it.tools.ComputerLocation;
import com.it.tools.NormalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 羡羡
 */
@Controller
@SessionAttributes({"error","uid"})
public class LoginColler {
    @Autowired
    UserService usserver;
    @Autowired
    LogService logse;

    /**
     * 登入
     * @param name
     * @param pass
     * @param map
     * @return
     */
    @RequestMapping("/login")
    public String login(String name, String pass, ModelMap map) throws NormalException {
        try {
            User ur=new User();
            ur.setUser(name);
            ur.setPass(pass);
            User us=usserver.login(ur);
            if(us!=null){
                map.put("uid",us.getId());
                map.put("error",null);
                map.put("img",us.getImg());
                //得到出生年月
                Date da=us.getBirthday();
                //计算
                int monmber=ComputerLocation.computer(da);
                //日期转换
                String formatDate = DateUtil.formatDate(da);
                //年
                String byear=formatDate.substring(0,4);
                //月
                String bmo=formatDate.substring(5,7);
                //日
                String bday=formatDate.substring(8,10);

                List confilis=logse.seal(us.getId());

                List lis=se(confilis);

                map.put("wz",monmber);
                map.put("year",byear);
                map.put("moth",bmo);
                map.put("day",bday);
                map.put("cofli",lis);
                return "index";
            }else{
                map.put("error","账号或密码错误！");
                return "redirect:login.jsp";
            }
        }catch (Exception e){
            throw new NormalException(e.getMessage());
        }
    }

    public List se(List lis){
        List lisr=new ArrayList();
        for(int i=0;i<lis.size();i++){
            Log loa= (Log) lis.get(i);
            lisr.add(loa.getLocation());
        }
        return lisr;
    }

    /**
     * 注册
     * @param us
     * @return
     */
    @RequestMapping("/regit")
    public String reg(User us) throws NormalException {
        System.out.println(us);
        try {
            int uaddsu=usserver.Useradd(us);
            if(uaddsu>0){
                return "redirect:login.jsp";
            }else{
                System.out.println("注册失败！");
                return "";
            }
        }catch (Exception e){
            throw new NormalException(e.getMessage());
        }
    }
}
